package com.cy.store.service.impl;

import com.cy.store.entity.Address;
import com.cy.store.entity.Cart;
import com.cy.store.entity.Product;
import com.cy.store.mapper.AddressMapper;
import com.cy.store.mapper.CartMapper;
import com.cy.store.mapper.ProductMapper;
import com.cy.store.service.IAddressService;
import com.cy.store.service.ICartService;
import com.cy.store.service.IDistrictService;
import com.cy.store.service.ex.*;
import com.cy.store.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class CartServImpl implements ICartService {


    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public void addCart(Integer uid,Integer amount,Integer pid,String userName) {
        Cart result = cartMapper.findProductByUid(uid, pid);
        Integer rows = null;
        if (result == null) {
            Cart cart = new Cart();
            cart.setPid(pid);
            cart.setUid(uid);
            //调用商品的持久层方法获取到指定商品的信息
            cart.setPrice(productMapper.getProductDetails(pid).getPrice());
            cart.setNum(amount);
            cart.setCreatedTime(new Date());
            cart.setCreatedUser(userName);
            rows = cartMapper.insertCart(cart);
            if (rows != 1) {
                throw new InsertException("插入数据过程中出现异常！");
            }
        } else {
            //通过uid和pid关联出cid
            rows = cartMapper.updateNum(result.getCid(), result.getNum() + amount, userName, new Date());
            if (rows != 1) {
                throw new UpdateException("数据更新过程中发生异常！");
            }
        }
    }

    @Override
    public List<CartVO> getCartProductByUid(Integer uid) {
        List<CartVO> list = cartMapper.findCartProductByUid(uid);
        return list;
    }

    @Override
    public Integer addNum(Integer uid, Integer cid, String userName) {
        Cart result = cartMapper.findCartByCid(cid);
        if(result==null){
            throw new CartNotExistException("购物车信息不存在！");
        }
        if(!result.getUid().equals(uid)){
            throw new AccessDeniedException("访问无权限异常！");
        }
        Integer rows = cartMapper.updateNum(cid,result.getNum()+1,userName,new Date());
        if(rows!=1){
            throw new UpdateException("更新数据过程中发生未知异常！");
        }
        return result.getNum()+1;
    }

    @Override
    public Integer reduceNum(Integer uid, Integer cid, String userName) {
        Cart result = cartMapper.findCartByCid(cid);
        if(result==null){
            throw new CartNotExistException("购物车信息不存在！");
        }
        if(!result.getUid().equals(uid)){
            throw new AccessDeniedException("访问无权限异常！");
        }
        Integer num = result.getNum()-1;
        if(num<0){
            throw new NumNotBelowZeroException("数量不能为负数！");
        }
        Integer rows = cartMapper.updateNum(cid,num,userName,new Date());
        if(rows!=1){
            throw new UpdateException("更新数据过程中发生未知异常！");
        }
        return num;
    }

    @Override
    public List<CartVO> getCartProductByCid(Integer uid, Integer[] cids) {
        if(cids==null){
            throw new NoSettleProductException("无可结算商品！");
        }
        List<CartVO> list = cartMapper.findCartProductByCid(cids);
        //迭代查询出的商品信息和当前登录用户进行判断，若不是则移除
        Iterator<CartVO> it =list.iterator();
        while(it.hasNext()){
            CartVO cart = it.next();
            if(!cart.getUid().equals(uid)){
                list.remove(cart);
            }
        }
        return list;
    }
}
